import { CrudOptions, AddReq, DelReq, EditReq, dict, CrudExpose, UserPageQuery, CreateCrudOptionsRet } from '@fast-crud/fast-crud';
import * as api from './api';
import { auth } from "/@/utils/authFunction";
import { ElMessage } from 'element-plus';

export default function ({ crudExpose }: { crudExpose: CrudExpose }): CreateCrudOptionsRet {
    const pageRequest = async (query: any) => {
        return await api.GetList(query);
    };
    const editRequest = async ({ form, row }: EditReq) => {
        if (row.id) {
            form.id = row.id;
        }
        return await api.UpdateObj(form);
    };
    const delRequest = async ({ row }: DelReq) => {
        return await api.DelObj(row.id);
    };
    const addRequest = async ({ form }: AddReq) => {
        return await api.AddObj(form);
    };

    const exportRequest = async (query: UserPageQuery) => {
        return await api.exportData(query)
    };

    return {
        crudOptions: {
            request: {
                pageRequest,
                addRequest,
                editRequest,
                delRequest,
            },
            actionbar: {
                buttons: {
                    export: {
                        show: auth('Device:Export'),
                        text: "导出",
                        title: "导出",
                        click() {
                            return exportRequest(crudExpose.getSearchFormData())
                        }
                    },
                    add: {
                        show: auth('Device:Create'),
                    },
                }
            },
            rowHandle: {
                fixed: 'right',
                width: 200,
                buttons: {
                    view: {
                        type: 'text',
                        order: 1,
                        show: auth('Device:Retrieve')
                    },
                    edit: {
                        type: 'text',
                        order: 2,
                        show: auth('Device:Update')
                    },
                    copy: {
                        type: 'text',
                        order: 3,
                        show: auth('Device:Copy')
                    },
                    remove: {
                        type: 'text',
                        order: 4,
                        show: auth('Device:Delete')
                    },
                },
            },
            form: {
                beforeSubmit({ form }) {
                    const lastDate = form.last_maintenance;
                    const nextDate = form.next_maintenance;
                    if (lastDate && nextDate) {
                        if (new Date(lastDate) >= new Date(nextDate)) {
                            ElMessage.error('上次维护时间必须早于下次维护时间');
                            return false;
                        }
                    }
                    if (nextDate && new Date(nextDate) < new Date()) {
                        ElMessage.error('下次维护时间不能早于当前时间');
                        return false;
                    }
                    return true;
                }
            },
            columns: {
                name: {
                    title: '设备名称',
                    type: 'input',
                    search: { show: true },
                    column: {
                        minWidth: 120,
                        sortable: 'custom',
                    },
                    form: {
                        helper: {
                            render() {
                                return <div style={"color:blue"}>设备名称为必填项</div>;
                            }
                        },
                        rules: [{ required: true, message: '设备名称必填' }],
                        component: {
                            placeholder: '请输入设备名称',
                        },
                    },
                },
                model: {
                    title: '设备型号',
                    type: 'input',
                    search: { show: true },
                    column: {
                        minWidth: 120,
                        sortable: 'custom',
                    },
                    form: {
                        rules: [{ required: true, message: '设备型号必填' }],
                        component: {
                            placeholder: '请输入设备型号',
                        },
                    },
                },
                status: {
                    title: '运行状态',
                    type: 'dict-select',
                    dict: dict({
                        data: [
                            { value: 1, label: '正常运行' },
                            { value: 2, label: '待维护' },
                            { value: 3, label: '维护中' },
                            { value: 4, label: '已停用' }
                        ]
                    }),
                    search: { show: true },
                    column: {
                        minWidth: 100,
                        sortable: 'custom',
                    },
                    form: {
                        rules: [{ required: true, message: '运行状态必选' }],
                    },
                },
                last_maintenance: {
                    title: '上次维护时间',
                    type: 'date',
                    search: { show: true },
                    form: {
                        component: {
                            format: "YYYY-MM-DD",
                            valueFormat: "YYYY-MM-DD",
                            placeholder: '请选择上次维护时间',
                        }
                    },
                    column: {
                        align: "center",
                        width: 120,
                        component: { name: "fs-date-format", format: "YYYY-MM-DD" }
                    }
                },
                next_maintenance: {
                    title: '下次维护时间',
                    type: 'date',
                    search: { show: true },
                    form: {
                        component: {
                            format: "YYYY-MM-DD",
                            valueFormat: "YYYY-MM-DD",
                            placeholder: '请选择下次维护时间',
                        },
                        rules: [
                            { required: true, message: '下次维护时间必填' }
                        ]
                    },
                    column: {
                        align: "center",
                        width: 120,
                        component: { name: "fs-date-format", format: "YYYY-MM-DD" }
                    }
                },
                description: {
                    title: '设备描述',
                    type: 'textarea',
                    search: { show: false },
                    column: {
                        minWidth: 200,
                    },
                    form: {
                        component: {
                            placeholder: '请输入设备描述',
                            showWordLimit: true,
                            maxlength: 500,
                        },
                    },
                },
            },
        },
    };
} 